package com.imc.platform.base.notice.service.impl;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.core.utils.HtmlFlagUtils;
import com.imc.platform.base.category.dao.ICategoryMapper;
import com.imc.platform.base.category.pojo.Category;
import com.imc.platform.base.notice.dao.INoticeMapper;
import com.imc.platform.base.notice.pojo.Notice;
import com.imc.platform.base.notice.pojo.NoticeAttach;
import com.imc.platform.base.notice.service.INoticeService;
import com.imc.platform.base.org.dao.IOrgMapper;
import com.imc.platform.base.user.dao.mapper.IUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述:
 * 开发者:SJC
 * 创建时间:2018-8-15 19:30:36
*/
@Slf4j
@Service
public class NoticeServiceImpl implements INoticeService {
    @Autowired
    INoticeMapper iNoticeMapper;

    @Autowired
    IOrgMapper iOrgMapper;
    @Autowired
    IUserMapper iUserMapper;
    @Autowired
    ICategoryMapper categoryMapper;

    /**
     * 方法描述:查询公告列表
     * SJC 2018-8-21 16:53:41
     *
    */
    @Override
    public PageInfo<Notice> queryNotice(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize,true);
        PageInfo d= new PageInfo<>(iNoticeMapper.queryNotice());
        return d;
    }

    /**
     * 方法描述: 增加公告标题 ，获取公告id
     * SJC 2018-8-21 16:54:00
     *
    */
    @Override
    public void addNoticeTitle(Notice nt) {
        int count = iNoticeMapper.addNoticeTitle(nt);
    }

    @Override
    public Notice queryOrderAndLeave(Map<String,Object>parms) {
        Notice notice=iNoticeMapper.queryOrderAndLeave(parms);
        notice.setContent(notice.getContent());
        iNoticeMapper.updaeChilckNum(String.valueOf(notice.getOrgId()));
        return notice;
    }


    /**
     * 方法描述：新增公告附件
     * SJC 2018-8-26 17:04:26
     *
     */
    @Override
    public int addNoticeAttach(NoticeAttach na){
        int count = iNoticeMapper.addNoticeAttach(na);
        return count;
    }

    /**
     * 方法描述：添加公告内容
     * SJC 2018-8-26 17:04:26
     *
     */
    @Override
    public int updateNoticeContent(String ntId, String contentDetail,List<String> memberList,String userId){
        Map<String,Object> parms=new HashMap();
        String acceptObj="";
        if(iNoticeMapper.querySendMember(ntId,userId).size()>0){
            acceptObj="2";
            if(iNoticeMapper.delNoticeByNotIdSendId(ntId,userId)==0){
                  return 0;
            }
        }else {
            acceptObj="1";
        }
        parms.put("noticeId",ntId);
        parms.put("sendId",userId);
        parms.put("memberList",memberList);
        if(iNoticeMapper.insertNoticeSendMember(parms)==0){
              return 0;
        }
        Notice notice = iNoticeMapper.queryNoticeById(ntId);
        if (notice.getNoticeType().equals("10")){
            contentDetail = contentDetail.replaceAll("<[.[^<]]*>", "");
        }
        return iNoticeMapper.updateNoticeContent(ntId, contentDetail,acceptObj);
    }

    /**
     * 方法描述：修改公告
     * SJC 2018-8-26 17:04:26
     *
     */
    @Override
    public int updateNotice(Notice nt){
        if(iNoticeMapper.querySendMember(String.valueOf(nt.getId()),nt.getAuthorId()).size()>0){
            int delCount=iNoticeMapper.delNoticeByNotIdSendId(String.valueOf(nt.getId()),nt.getAuthorId());
            if(delCount<0){
                return 0;
            }
        }
        Map<String,Object> parms=new HashMap();
        parms.put("noticeId",nt.getId());
        parms.put("sendId",nt.getAuthorId());
        parms.put("memberList",nt.getSendIdList());
        if(iNoticeMapper.insertNoticeSendMember(parms)<0){
            return 0;
        }
        int count = iNoticeMapper.updateNotice(nt);
        return count;
    }


    /**
     * 方法描述:获取图片列表
     * SJC 2018-9-3 21:42:33
     *
     */
    @Override
    public List<NoticeAttach> queryNoticeAttachList(String ntId){
        List<NoticeAttach> naList  = iNoticeMapper.queryAttachByNtId(ntId);
        return naList;
    }

    /**
     * 方法描述:根据id获取公告
     * SJC 2018-9-4 10:39:58
     *
     */
    @Override
    public Notice queryNoticeById(String ntId){
        return iNoticeMapper.queryNoticeById(ntId);
    }



    /**
     * 方法描述:删除附件
     * SJC 2018-9-4 17:21:36
     *
     */
    @Override
    public int delNoticeAttach(String ntId,String imgName){
        int count= iNoticeMapper.delNoticeAttach(ntId,imgName);
        return count;
    }


    /**
     * 方法描述:删除公告
     * SJC 2018-9-4 21:53:49
     *
     */
    @Override
    public int delNotice(String ntId){
        iNoticeMapper.delNoticeAttachs(ntId);
        return iNoticeMapper.delNotice(ntId);
    }



    /**
     * 方法描述:根据类型查询公告列表信息
     * SJC 2018-9-11 16:25:24
     *
     */
    @Override
    public List<Map<String,Object>> queryNoticeList(String noticeType){

        return iNoticeMapper.queryNoticeList(noticeType);
    }


    /**
     * 方法描述:查询所有类别
     * SJC 2018-9-13 16:34:30
     *
     */
    @Override
    public List<Category> queryCategory(){
        return iNoticeMapper.queryCategory();
    }


    /**
     * 方法描述:插入新增发送公告人员
     * SJC 2018-9-13 20:08:04
     *
     */
    @Override
    public int insertNoticeSend(String noticeId,String sendId,List<String> memberList){
        // 先剔除已经加过的
        List<String> sendMem = iNoticeMapper.querySendMember(noticeId,sendId);
        memberList.removeAll(sendMem);
        if(memberList.size() != 0){

            Map<String ,Object> map = new HashMap<String,Object>();
            map.put("noticeId",noticeId);
            map.put("sendId",sendId);
            map.put("memberList",memberList);
            return iNoticeMapper.insertNoticeSendMember(map);
        }else{
            return 1;
        }
    }

    /**
     * 方法描述:查询已经发送的用户
     * SJC 2018-9-14 15:41:53
     *
     */
    public List<String> querySendMember(String noticeId,String senderId){
        return iNoticeMapper.querySendMember(noticeId,senderId);
    }


    /**
     * 方法描述:根据公告id获取所有已经发送的用户
     * SJC 2018-9-14 17:31:58
     *
     */
    public List<Map<String,Object>> queryReceivers(String noticeId){
        return iNoticeMapper.queryReceivers(noticeId);
    }

    @Override
    public List queryNoticeListByTypeAndId(Map<String, Object> parms) {
        if("3".equals(parms.get("type"))){
            return iOrgMapper.queryOrg();
        }else if ("28".equals(parms.get("type"))){
            PageHelper.startPage(Integer.parseInt(parms.get("number").toString())+1,Integer.parseInt(parms.get("size").toString()));
            return iUserMapper.queryUserNoStudent(parms.get("id").toString());
        }else {
                List list;
                PageHelper.startPage(Integer.parseInt(parms.get("number").toString())+1,Integer.parseInt(parms.get("size").toString()));
                list =iNoticeMapper.queryNoticeListByTypeAndId(parms);
                List<Notice>result=new ArrayList<>();
                if(list.size()>0){
                    for(Object object : list){
                        Notice notice=(Notice) object;
                        notice.setAuthorName("桃李舞苑");
                        String content=notice.getContent();
                        if (!StringUtils.isEmpty(content) && StringUtils.isEmpty(parms.get("id").toString())){
                            content = HtmlFlagUtils.removeHtmlTag(content);
                            int len=content.length() >120 ? 120 : content.length()-1;
                            notice.setContent(content.substring(0,len));
                        }else if (!StringUtils.isEmpty(content) && StringUtils.isNotEmpty(parms.get("id").toString())){
                            notice.setContent(content);
                        }
                        result.add(notice);
                    }
                }
                return result;
        }
    }

    @Override
    public Object queryNoticeDetByTypeAndId(Map<String, Object> parms) {
//        Object object=new Object();

        return iNoticeMapper.queryNoticeDetByTypeAndId(parms);
    }

    @Override
    public List<Category> querySelectCatList() {
        return categoryMapper.querySelectCatList();
    }
}
